import React, { useEffect, useState, useRef } from 'react'

function useDao(count = 60) {
    const [num, setNum] = useState(count)
    let timer=useRef(null)
    const [flag,setFlag]=useState(false)
    const fn = () => {
        setFlag(true)
        timer.current=setInterval(() => {
            setNum((c) => c - 1)
        }, 1000)
    }

    useEffect(()=>{
        if(num<=0){
            clearInterval(timer.current)
            setNum(count)
            setFlag(false)
        }
    },[fn,num])

    return { fn, num ,flag}

}

export default useDao
